package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.SafetyCardDao;
import cn.hls.entity.SafetyCard;
import cn.hls.entity.vo.PageInfo;
import cn.hls.exception.BussinessException;
import cn.hlsconstant.DataAssessEnum;
 
/**
  *@fileName: SafetyCardDaoImpl.java
  *@package: cn.hls.dao.impl
　*@ Description:货物安全卡数据操作接口实现类
　*@author: uuhui@163.com
  *@date: 2016年6月9日
 *
 */
@Component("safetyCardDao")
public class SafetyCardDaoImpl extends BaseDaoImpl<SafetyCard> implements SafetyCardDao {

	@Override
	public void delete(Object entityId) throws Exception {
		String []ids=(String[])entityId;
		StringBuilder hql=new StringBuilder("delete SafetyCard  where id in (");
		List<Object>params=new ArrayList<Object>();
		for (String id:ids){
			hql.append("?,");
			params.add(Long.valueOf(id));
		}	
		hql.delete(hql.length()-1, hql.length());
		hql.append(")");
		 try {
			executeHQLUpdate(hql.toString(), params.toArray());
		} catch (Exception e) {
			e.printStackTrace();
			throw new BussinessException(DataAssessEnum.UPDATE_ERROR,"删除信息失败" );
		}
	}
	@Override
	public PageInfo<SafetyCard> loadSafetyCardInfo(PageInfo<SafetyCard> page,
			SafetyCard entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from SafetyCard t where t.deleted=? ");
		StringBuilder countHql=new StringBuilder("select count(id) ");
		List<Object> params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按名称查询
			if(StringUtils.hasText(entity.getName())){
				selectHql.append("and t.name like ? ");
				params.add("%"+entity.getName().trim()+"%");
			}
			//按编码查询
			if(StringUtils.hasText(entity.getCode())){
				selectHql.append("and t.code like ? ");
				params.add("%"+entity.getCode().trim()+"%");
			}
		}
		countHql.append(selectHql);
		selectHql.append("order by id desc" );
		return find(selectHql.toString(), countHql.toString(), params.toArray(), page);
	}

	@Override
	public boolean exists(SafetyCard entity)throws Exception{
		 StringBuilder hql=new StringBuilder("select count(id) from SafetyCard where deleted=? ");
		 List<Object>params=new ArrayList<>();
		 params.add(false);
		 if(entity.getId()!=null){//更新操作时，排除本记录
			 hql.append("and id!=? ");
			 params.add(entity.getId());
		 }
		 if(StringUtils.hasText(entity.getName2())){//存在说明书的内容时
			 hql.append("and (name=? or name2=? )");
			 params.add(entity.getName().trim());
			 params.add(entity.getName2().trim());
		 }else{
			 hql.append("and name=?");
			 params.add(entity.getName().trim());
		 }
		Long c=countByHql(hql.toString(), params.toArray());
		return c.intValue()>0?true:false;
	}

	@Override
	public List<SafetyCard> loadSafetyCardInfo(SafetyCard entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from SafetyCard t where t.deleted=? ");
		List<Object> params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按名称查询
			if(StringUtils.hasText(entity.getName())){
				selectHql.append("and t.name like ? ");
				params.add("%"+entity.getName().trim()+"%");
			}
			 
			//按编码查询
			if(StringUtils.hasText(entity.getCode())){
				selectHql.append("and t.code like ? ");
				params.add("%"+entity.getCode().trim()+"%");
			}
			 
		}
		return findByHql(selectHql.toString(), params.toArray());
	}
}
